//
//  RGMeSettingVC.m
//  RedGrass
//
//  Created by wuyine on 2018/12/13.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMeSettingVC.h"
#import "RGMeSettingCell.h"
#import "RGMePersonalInfoEditVC.h"
#import "RGShareProfileView.h"
#import "RGQuitLoginView.h"
#import "RGMeWalletVC.h"
#import "RGBaseTextShowViewController.h"
#import "RGMeAboutVC.h"
#import "RGInviteVC.h"
#import <SDImageCache.h>
#import "AppDelegate+APPService.h"
#import "XLMShareSdkTool.h"
#import "SocketRocketUtility.h"


@interface RGMeSettingVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) RGShareProfileView *shareProfileView;
@property (nonatomic,strong) RGQuitLoginView *quitLoginView;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation RGMeSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customNavBar.title = @"设置";
    [self showBackButton];
    self.customContentView.backgroundColor = [UIColor blackColor];
    [self.customContentView addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 3){
        return 0.1f;
    }
    return 55;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RGMeSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"settingCell"];
    switch (indexPath.row) {
        case 0: {
            cell.icon.image = IMAGE_NAMED(@"编辑个人资料");
            cell.title.text = @"编辑个人资料";
        }
            break;
        case 1: {
            cell.icon.image = IMAGE_NAMED(@"分享个人主页");
            cell.title.text = @"分享个人主页";
        }
            break;
        case 2: {
            cell.icon.image = IMAGE_NAMED(@"邀请有礼");
            cell.title.text = @"邀请有礼";
        }
            break;
        case 3: {
            cell.icon.image = IMAGE_NAMED(@"检查更新");
            cell.title.text = @"检查更新";
        }
            break;
        case 4: {
            cell.icon.image = IMAGE_NAMED(@"钱包");
            cell.title.text = @"存钱罐";
        }
            break;
        case 5: {
            cell.icon.image = IMAGE_NAMED(@"用户协议");
            cell.title.text = @"用户协议";
        }
            break;
        case 6: {
            cell.icon.image = IMAGE_NAMED(@"社区自律公约");
            cell.title.text = @"社区自律公约";
        }
            break;
        case 7: {
            cell.icon.image = IMAGE_NAMED(@"隐私政策");
            cell.title.text = @"隐私政策";
        }
            break;
        case 8: {
            cell.icon.image = IMAGE_NAMED(@"关于");
            cell.title.text = @"关于";
        }
            break;
        case 9: {
            cell.icon.image = IMAGE_NAMED(@"清理缓存");
            cell.title.text = @"清理缓存";
        }
            break;
        case 10: {
            cell.icon.image = IMAGE_NAMED(@"退出");
            cell.title.text = @"退出";
        }
            break;
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseViewController *vc;
    switch (indexPath.row) {
        case 0: {
            vc = [[RGMePersonalInfoEditVC alloc] init];
        }
            break;
        
        case 1: {
            [self.shareProfileView showInView:self.customContentView];
            NSString *nick = [RGUserInfoModel getUserInfo].nickname;
            self.shareProfileView.nick = SafeStr(nick);
            return ;
        }
            break;
        case 2: {
            vc = [[RGInviteVC alloc] init];
        }
            break;
        case 4: {
            vc = [[RGMeWalletVC alloc] init];
        }
            break;
        case 5:{
            vc = [RGBaseTextShowViewController new];
            vc.customNavBar.title = @"用户协议";
        }
            break;
        case 6:{
            vc = [RGBaseTextShowViewController new];
            vc.customNavBar.title = @"社区自律公约";
        }
            break;
        case 7:{
            vc = [RGBaseTextShowViewController new];
            vc.customNavBar.title = @"隐私政策";
        }
            break;
        case 8:{
            vc = [[RGMeAboutVC alloc] init];
        }
            break;
        case 9:{
            [self showLoading:@"正在清理缓存"];
            [self removeCacheFile];
            
        }
            break;
        case 10: {
            [self.quitLoginView showInView:self.navigationController.view];
            [self.quitLoginView setQuitLogin:^{
                RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
                user.token = @"";
                [RGUserInfoModel storeUserInfo:user];
                [[SocketRocketUtility instance] SRWebSocketClose];
                [[AppDelegate shareAppDelegate] reInitTabarViewController];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationLoginStateChange object:[NSNumber numberWithBool:NO]];
            }];
            return ;
        }
            break;
        default:
            break;
    }
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 11;
}

//清除视频缓存
-(void) removeCacheFile{
   //清除视频
    NSString *VedioFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/TXCache"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:VedioFilePath] == YES) {
        [[NSFileManager defaultManager] removeItemAtPath:VedioFilePath error:nil]; //发布成功，删除视频文件
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    
    for (NSString *p in files) {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
            //清理缓存，保留Preference，里面含有NSUserDefaults保存的信息
            if (![Path containsString:@"Preferences"]) {
                [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
            }
        }
    }
    //清除图片
    AA_WEAK_MAKE(self) weakSelf = self;
    [[SDImageCache sharedImageCache] clearMemory];
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
        [weakSelf dismissLoading];
        [weakSelf showTextMessageWithMessage:@"清除成功"];
    }];
}

- (RGShareProfileView *)shareProfileView {
    if (!_shareProfileView) {
        _shareProfileView = [[RGShareProfileView alloc] init];
        AA_WEAK_MAKE(self) weakSelf = self;
        [_shareProfileView setShareUserWithShareImgBlock:^(UIImage * _Nonnull image) {
            if(image){
                [XLMShareSdkTool shareWechatTimelineContentWithShareContentType:SSDKContentTypeText contentImage:image onStateChanged:^(SSDKResponseState state) {
                    if(state == SSDKResponseStateSuccess){
                        [weakSelf showTextMessageWithMessage:@"分享成功"];
                    }
                }];
            }else{
                [weakSelf showTextMessageWithMessage:@"分享失败"];
            }
        }];
    }
    return _shareProfileView;
}

- (RGQuitLoginView *)quitLoginView {
    if (!_quitLoginView) {
        _quitLoginView = [[RGQuitLoginView alloc] init];
    }
    return _quitLoginView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
        [_tableView registerClass:[RGMeSettingCell class] forCellReuseIdentifier:@"settingCell"];
    }
    return _tableView;
}

@end
